home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / PGM_TOOL / RLINE_OP / RLTEST.PAS < prev    next >
Pascal/Delphi Source File  |  1989-10-09  |  6KB  |  245 lines

  1. PROGRAM RLtest;
  2.   { Test program for the RLINE unit.
  3.  
  4.   Does a speed comparison between FReadLn and ReadLn,
  5.        a file position/seek test,
  6.        and types a file to the screen.
  7.  
  8.   Test with different files and buffer sizes (CONST BS, below).
  9.   }
  10.  
  11. USES
  12.   DOS, CRT, RLINE;
  13.  
  14.   { Global constants and variables.}
  15. CONST
  16.   BS      = 8192;            { Disk Buffer size. }
  17.  
  18. TYPE
  19.   RFtester = Object(RFextended)
  20.     PROCEDURE CheckRFerror; virtual;
  21.   END;
  22.  
  23.   PROCEDURE RFtester.CheckRFerror;
  24.     { Displays some of the common errors, and waits for a keypress. }
  25.   VAR
  26.     S       : STRING[80];
  27.   BEGIN
  28.     IF RFerror = 0 then exit;
  29.     WriteLn(RFerrorString);
  30.     IF (RFerror <> $FFFF)
  31.     THEN Halt(1);
  32.   END;
  33.  
  34. VAR
  35.   TBuf    : ARRAY[1..BS] OF Char;
  36.  
  37. PROCEDURE PressAnyKey;
  38. BEGIN
  39.   Writeln('Press any key.');
  40.   While ReadKey = #0 Do ;
  41. END;
  42.  
  43.   { Timing routine.  Derived from Neil Rubenking's TIMER.PAS in LIB 4. }
  44. TYPE
  45.   OnOrOff = (On, Off);
  46.  
  47. VAR
  48.   start, time : Real;
  49.  
  50.   PROCEDURE timer(O : OnOrOff);
  51.   VAR
  52.     hour, min, sec, hun : Word;
  53.   BEGIN
  54.     GetTime(hour, min, sec, hun);
  55.     time := hour*3600+min*60+sec+hun/100;
  56.     CASE O OF
  57.       On : start := time;
  58.       Off : BEGIN
  59.               time := time-start;
  60.               Write('Time: ', time:6:2, ' ');
  61.             END;
  62.     END;
  63.   END;
  64.  
  65.   (************************************************************************)
  66.  
  67.   PROCEDURE PrepForTimingTest(Fn : STRING);
  68.     { Opens and read Fn, before doing the FReadLn/ReadLn timing tests.
  69.     Otherwise, the order the two tests are performed produces different
  70.     results ( probably because the disk heads start in different positions,
  71.     and maybe second test benefits from using previously filled DOS buffers. }
  72.   VAR
  73.     i  : Integer;
  74.     j  : LongInt;
  75.     RF : RFtester;
  76.     S  : String;
  77.   BEGIN
  78.     WriteLn('Reading file to prepare for timing tests..');
  79.     RF.Init(Fn, BS, TBuf);
  80.     RF.CheckRFerror;
  81.     WHILE RF.RFerror = 0 DO RF.FReadLn(S);
  82.     RF.Done;
  83.   END;
  84.  
  85.   PROCEDURE ReadLnTest(Fn : STRING);
  86.     { Time comparison between FReadLn and ReadLn }
  87.   VAR
  88.     NLines  : LongInt;
  89.     Ch : char;
  90.     RF : RFtester;
  91.     S  : String;
  92.     F  : Text;
  93.     i  : Integer;
  94.   BEGIN
  95.     {Test FReadLn}
  96.     IF Not RF.Init(Fn, BS, TBuf) THEN BEGIN
  97.       Writeln('Not enough memory.');
  98.       Halt(1);
  99.     END;
  100.     RF.CheckRFerror;
  101.  
  102.     Writeln('FReadLn timing test: Reading strings from ', Fn, '.. ');
  103.     NLines := 0;
  104.     timer(On);
  105.  
  106.     RF.FReadLn(S);
  107.     While RF.RFerror = 0 DO BEGIN
  108.       Inc(NLines);
  109.       RF.FReadLn(S);
  110.     END;
  111.     RF.CheckRFerror;
  112.     timer(Off); WriteLn;
  113.     Writeln(NLines, ' lines were read.');
  114.  
  115.     WriteLn;
  116.  
  117.     {Test TP ReadLn}
  118.     Assign(f, Fn);
  119.     Reset(f);
  120.     RF.RFerror := IoResult;
  121.     RF.CheckRFerror;
  122.  
  123.     WriteLn('ReadLn timing test: Reading strings from ', Fn, '... ');
  124.     SetTextBuf(f, TBuf);
  125.     NLines := 0;
  126.     timer(On);
  127.     REPEAT
  128.       ReadLn(f, S);
  129.       i := IoResult;
  130.       IF i = 0
  131.       THEN Inc(NLines);
  132.     UNTIL EOF(F) OR (i <> 0);
  133.     timer(Off); WriteLn;
  134.     WriteLn(NLines, ' lines were read.   IoResult = ',i);
  135.  
  136.     writeln;
  137.     {Test FRead}
  138.     RF.Reset;
  139.     RF.CheckRFerror;
  140.  
  141.     WriteLn('FRead timing test: Reading chars from ', Fn, '.. ');
  142.     NLines := 0;
  143.     timer(On);
  144.     RF.FRead(ch);
  145.     While RF.RFerror = 0 DO BEGIN
  146.       Inc(NLines);
  147.       RF.FRead(ch);
  148.     END;
  149.     timer(Off); WriteLn;
  150.     Write(NLines, ' chars were read.');
  151.     RF.CheckRFerror;
  152.     RF.Done;
  153.   END;
  154.  
  155.  
  156.   PROCEDURE TypeFile(Fn : STRING);
  157.     { TYPE a file to the screen.  A useless procedure except that it
  158.     demonstrates using a buffer allocated on the heap to be used by RLINE. }
  159.   VAR
  160.     RF   : RFtester;         { Declare RFrec variable. }
  161.     TBuf : Pointer;
  162.     S    : String;
  163.   BEGIN
  164.     ClrScr;
  165.     GetMem(TBuf, BS);        { First, allocate memory for the buffer. }
  166.  
  167.     { Be certain to insert the ^ in TBuf^ when opening the file. }
  168.     RF.Init(Fn, BS, TBuf^); { try to open the file. }
  169.     RF.CheckRFerror;
  170.  
  171.     RF.FReadLn(S);
  172.     While RF.RFerror = 0 DO BEGIN
  173.       IF keypressed AND (readkey = ^S) { if user pressed ^S, then pause }
  174.       THEN IF readkey <> #0 THEN ; { the display by forcing a keypress. }
  175.  
  176.       WriteLn(S);       { if no error, then display the line. }
  177.       RF.FReadLn(S);    { Attempt to read the next line from the file. }
  178.     END;
  179.     RF.CheckRFerror;
  180.     RF.Done;
  181.     FreeMem(TBuf, BS);       { Deallocate memory for the buffer. }
  182.   END;
  183.  
  184.  
  185.   PROCEDURE PositioningTest(Fn : STRING);
  186.   VAR
  187.     NLines, lno : LongInt;
  188.     ch      : Char;
  189.     RF : RFtester;
  190.     S : String;
  191.   BEGIN
  192.     ClrScr;
  193.     WriteLn('     Pos    Line     Pos    Line     Pos    Line     Pos    Line     Pos    Line');
  194.  
  195.     RF.Init(Fn, BS, TBuf);   { Open Fn }
  196.     RF.CheckRFerror;
  197.  
  198.     window(1, 2, 80, 25);
  199.     NLines := 0;
  200.     Write(RF.FFilepos:8, NLines:8);
  201.     RF.FReadLn(S);
  202.     While RF.RFerror = 0 Do BEGIN
  203.       Inc(NLines);
  204.       Write(RF.FFilepos:8, NLines:8);
  205.       RF.FReadLn(S);
  206.     END;
  207.  
  208.     WriteLn(^j^j^j^j);
  209.     window(1, 21, 80, 25);
  210.  
  211.     REPEAT
  212.       Write('(10000 to quit) Seek to: '); ReadLn(lno);
  213.       RF.fseek(lno);
  214.       IF RF.RFerror = 0 THEN BEGIN
  215.         RF.FRead(ch);   RF.CheckRFerror;
  216.         WriteLn('Char is: #', Ord(ch));
  217.         RF.fseek(lno); RF.CheckRFerror;
  218.         RF.FReadLn(S); RF.CheckRFerror;
  219.         WriteLn(S);
  220.       END ELSE Writeln(RF.RFerrorString);
  221.     UNTIL lno = 10000;
  222.     RF.Done;
  223.     window(1, 1, 80, 25);
  224.   END;
  225.  
  226.  
  227. BEGIN
  228.   WriteLn;
  229.   IF ParamCount = 0 THEN BEGIN
  230.     Write('You must specify a Filename on command line.');
  231.     Halt(1);
  232.   END;
  233.  
  234.   PrepForTimingTest(ParamStr(1));
  235.  
  236.   ReadLnTest(ParamStr(1));
  237.  
  238.   Pressanykey;
  239.  
  240.   IF ParamCount > 1
  241.   THEN PositioningTest(ParamStr(2))
  242.   ELSE PositioningTest(ParamStr(1));
  243.  
  244.   TypeFile(ParamStr(1));
  245. END.